package com.ruoyi.project.per.emp.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.per.emp.domain.Emp;
import com.ruoyi.project.per.emp.service.IEmpService;
import com.ruoyi.project.system.post.service.IPostService;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小新
 * @date 2020-09-17 11:37
 * @desc
 */
@Controller
@RequestMapping("/per/emp")
public class EmpController extends BaseController {

    @Autowired
    private IEmpService empService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPostService postService;

    private String prefix = "per/emp";
    @RequiresPermissions("per:emp:view")
    @GetMapping()
    public String emp()
    {
        return prefix + "/emp";
    }

    /**
     * 查询人员信息
     * @param emp
     * @return
     */
    @RequiresPermissions("per:emp:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Emp emp)
    {
        startPage();
        List<Emp> list = empService.selectEmpList(emp);
//       list.forEach(e-> System.out.println("成功"+e));
        return getDataTable(list);
    }

    /**
     * 新增员工
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("roles", roleService.selectRoleAll());
        mmap.put("posts", postService.selectPostAll());
        return prefix + "/add";
    }
    /**
     * 新增保存员工
     */
    @RequiresPermissions("per:emp:add")
    @Log(title = "人员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Emp emp)
    {


           if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(empService.checkPhoneUnique(emp)))
        {
            return error("新增用户'" + emp.getEmpName() + "'失败，手机号码已存在");
        }
            else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(empService.checkEmailUnique(emp)))
        {
            return error("新增用户'" + emp.getEmpName() + "'失败，邮箱账号已存在");
        }
            return toAjax(empService.insertEmp(emp));
    }


    /**
     * 修改用户
     */
    @GetMapping("/edit/{empId}")
    public String edit(@PathVariable("empId") Long empId, ModelMap mmap)
    {
        mmap.put("emp", empService.selectEmpById(empId));
        System.out.println(mmap.get("emp"));
        mmap.put("posts", postService.selectPostsByEmpId(empId));
        System.out.println(mmap.get("posts")+"post");
        return prefix + "/edit";
    }
        /**
         * 修改保存用户
         */
        @RequiresPermissions("system:user:edit")
        @Log(title = "用户管理", businessType = BusinessType.UPDATE)
        @PostMapping("/edit")
        @ResponseBody
        public AjaxResult editSave(@Validated Emp emp)
        {
            System.out.println(emp.getStatus()+"******************");
            if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(empService.checkPhoneUnique(emp)))
            {
                return error("修改用户'" + emp.getEmpName() + "'失败，手机号码已存在");
            }
            else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(empService.checkEmailUnique(emp)))
            {
                return error("修改用户'" + emp.getEmpName() + "'失败，邮箱账号已存在");
            }
            return toAjax(empService.updateEmp(emp));
        }

    /**
     * 删除员工
     * @param ids
     * @return
     */
    @RequiresPermissions("per:emp:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(empService.deleteEmpByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(Emp emp)
    {
        return empService.checkPhoneUnique(emp);
    }

    /**
     * 校验email邮箱
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(Emp emp)
    {
        return empService.checkEmailUnique(emp);
    }

}
